package com.woniu.custom.config;

import nu.pattern.OpenCV;
import org.opencv.core.*;
import org.opencv.highgui.HighGui;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
import org.opencv.objdetect.CascadeClassifier;

/**
 * 描述：Java中利用OpenCV进行人脸识别
 *
 * @author
 */
public class FaceRecognition
{
    public static void main(String[] args)
    {
        // 加载 OpenCV 库
        OpenCV.loadShared();

        // 读取图像
        String imagePath = "D://touxiang/zyr.jpg";
        Mat image = Imgcodecs.imread(imagePath);

        // 加载人脸分类器
        String xmlFile = "D://opencvv//opencv//data//haarcascades/haarcascade_frontalface_alt.xml";
        CascadeClassifier faceDetector = new CascadeClassifier(xmlFile);

        // 人脸检测
        MatOfRect faceDetections = new MatOfRect();
        faceDetector.detectMultiScale(image, faceDetections);

        // 绘制人脸框
        for (Rect rect : faceDetections.toArray())
        {
            Imgproc.rectangle(image, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height), new Scalar(0, 255, 0));
        }

        // 显示结果
        HighGui.imshow("Detected Face", image);
        HighGui.waitKey();

        // 释放资源
        image.release();
    }
}


